package wangjg.coco.module;

import java.io.File;
import java.util.List;

import wangjg.commons.bean.BeanUtil;
import wangjg.mda.model.obj.Model;
import wangjg.mda.tra.ModelTrans;

public class Source2ModelProcessor extends BasicProcessor {
	
	private boolean includeField = true;;
	private boolean includeMethod = true;
	private String filter_exp;

	public Source2ModelProcessor() {
	}
	
	public void saveToModel(String targetDir){
		ModelTrans mu = new ModelTrans();
		List<Model> modelList = this.getModelList();
		for(Model model:modelList){
			if(!this.includeField){
				model.setFields(null);
			}
			if(!this.includeMethod){
				model.setMethods(null);
			}
			if(filter_exp!=null && filter_exp.length()>0){
				String[] exps = filter_exp.split("[,\\s]+");
				for(String nestedProp : exps){
					nestedProp = nestedProp.trim();
					if(nestedProp.length()>0){
						try{
							BeanUtil.updateProperty(model, nestedProp, null);	
						}catch(NumberFormatException e){
							e.printStackTrace();
						}
					}
				}	
			}
			mu.saveModel(model, targetDir+File.separator+model.getName()+".xml");
		}
	}

	public boolean isIncludeField() {
		return includeField;
	}

	public void setIncludeField(boolean includeField) {
		this.includeField = includeField;
	}

	public boolean isIncludeMethod() {
		return includeMethod;
	}

	public void setIncludeMethod(boolean includeMethod) {
		this.includeMethod = includeMethod;
	}

	public String getFilter_exp() {
		return filter_exp;
	}

	public void setFilter_exp(String filter_exp) {
		this.filter_exp = filter_exp;
	}

}
